<!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/extras/chrome/chrome_user_friendly_category_driver.html">
<link rel="import" href="/tracing/metrics/system_health/long_tasks_metric.html">
<link rel="import" href="/tracing/value/histogram_set.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  test('longTasksMetric', function() {
    const model = tr.c.TestUtils.newModel(function(model) {
      const proc = model.getOrCreateProcess(1);
      const thread = proc.getOrCreateThread(2);
      thread.name = 'CrRendererMain';
      const longTask = tr.c.TestUtils.newSliceEx({
        title: 'foo',
        start: 0,
        duration: 50,
      });
      thread.sliceGroup.pushSlice(longTask);

      thread.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx({
        title: 'UpdateLayerTree',
        start: 0,
        duration: 1,
        cpuStart: 0,
        cpuDuration: 1,
      }));

      thread.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx({
        title: 'minorGC',
        start: 1,
        duration: 1,
        cpuStart: 1,
        cpuDuration: 1,
      }));

      thread.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx({
        title: 'Decode Image',
        start: 2,
        duration: 1,
        cpuStart: 2,
        cpuDuration: 1,
      }));

      thread.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx({
        title: 'Layout',
        start: 3,
        duration: 1,
        cpuStart: 3,
        cpuDuration: 1,
      }));

      model.addUserFriendlyCategoryDriver(
          tr.e.chrome.ChromeUserFriendlyCategoryDriver);
    });

    const histograms = new tr.v.HistogramSet();
    tr.metrics.sh.longTasksMetric(histograms, model);

    const longTaskHist = histograms.getHistogramNamed('longTasks');
    assert.strictEqual(1, longTaskHist.numValues);

    const relatedNames = longTaskHist.diagnostics.get('categories');
    assert.instanceOf(relatedNames, tr.v.d.RelatedNameMap);
    assert.strictEqual(relatedNames.get('layout'), 'longTasks:layout');
    assert.strictEqual(relatedNames.get('gc'), 'longTasks:gc');
    assert.strictEqual(relatedNames.get('composite'), 'longTasks:composite');
    assert.strictEqual(relatedNames.get('imageDecode'),
        'longTasks:imageDecode');

    const bin = longTaskHist.getBinForValue(longTaskHist.average);
    const diagnostics = tr.b.getOnlyElement(bin.diagnosticMaps);
    const breakdown = diagnostics.get('categories');
    assert.instanceOf(breakdown, tr.v.d.Breakdown);
    assert.strictEqual(breakdown.size, 4);

    const taskEventSet = diagnostics.get('events');
    assert.instanceOf(taskEventSet, tr.v.d.RelatedEventSet);
    assert.lengthOf(taskEventSet, 1);

    const layoutHist = histograms.getHistogramNamed('longTasks:layout');
    assert.strictEqual(1, layoutHist.numValues);
    assert.lengthOf(tr.b.getOnlyElement(layoutHist.getBinForValue(
        layoutHist.average).diagnosticMaps).get('events'), 1);

    const gcHist = histograms.getHistogramNamed('longTasks:gc');
    assert.strictEqual(1, gcHist.numValues);
    assert.lengthOf(tr.b.getOnlyElement(gcHist.getBinForValue(
        gcHist.average).diagnosticMaps).get('events'), 1);

    const compositeHist = histograms.getHistogramNamed('longTasks:composite');
    assert.strictEqual(1, compositeHist.numValues);
    assert.lengthOf(tr.b.getOnlyElement(compositeHist.getBinForValue(
        compositeHist.average).diagnosticMaps).get('events'), 1);

    const imageDecodeHist = histograms.getHistogramNamed(
        'longTasks:imageDecode');
    assert.strictEqual(1, imageDecodeHist.numValues);
    assert.lengthOf(tr.b.getOnlyElement(imageDecodeHist.getBinForValue(
        imageDecodeHist.average).diagnosticMaps).get('events'), 1);
  });
});
</script>
